package com.zlm.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * @author zlm
 * @date 2025/4/27
 */
public class TestField {
    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("com.zlm.reflect.Student");
        //testGetFields(aClass);
        testGetField(aClass);
    }

    public static void testGetFields(Class<?> aClass)throws Exception{

        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }
    }
    public static void testGetField(Class<?> aClass)throws Exception{
        Field name = aClass.getDeclaredField("name");
        Constructor<?> constructor = aClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Object obj = constructor.newInstance();
        name.setAccessible(true);
        name.set(obj,"zlm");
        System.out.println("属性名"+name.getName());
        System.out.println("属性类型"+name.getType());
        System.out.println("属性值"+name.get(obj));
        System.out.println("对象"+obj);
    }
}
